package com.woniu.gf.pawn.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.gf.pawn.comm.core.domain.Product;
import com.woniu.gf.pawn.comm.core.domain.ProductProperty;
import com.woniu.gf.pawn.comm.core.domain.ProductPropertyValue;
import com.woniu.gf.pawn.dao.ProductPropertyMapper;
import com.woniu.gf.pawn.dao.ProductPropertyValueMapper;
import com.woniu.gf.pawn.product.parma.FindProductBigTypePageParam;
import com.woniu.gf.pawn.product.parma.ShowProductPropertyParam;
import com.woniu.gf.pawn.service.IProductPropertyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-10-15
 */
@Service
public class ProductPropertyServiceImpl extends ServiceImpl<ProductPropertyMapper, ProductProperty> implements IProductPropertyService {
    @Autowired
    private ProductPropertyValueMapper productPropertyValueMapper;
    @Autowired
    private ProductPropertyMapper productPropertyMapper;

    /**
     * 添加
     * @param productProperty
     * @param productPropertyValueList
     */
    @Override
    @Transactional
    public void addProductProperty(ProductProperty productProperty, List<ProductPropertyValue> productPropertyValueList) {

        productPropertyMapper.insert(productProperty);
        for (int i=0;i<productPropertyValueList.size();i++){
            productPropertyValueList.get(i).setProductPropertyId(productProperty.getProductPropertyId());
        }
        productPropertyValueMapper.addProductPropertyValues(productPropertyValueList);
    }

    /**
     * 分页
     *
     * @param pages
     * @return
     */
    @Override
    public IPage<ShowProductPropertyParam> selectPageVo(FindProductBigTypePageParam pages) {

        Page<ShowProductPropertyParam> productPropertyPage = new Page<>(pages.getIndex(), pages.getSize());

        IPage<ShowProductPropertyParam> productIPage = productPropertyMapper.selectPageVo(productPropertyPage, pages.getName());

        if(productIPage!=null&&productIPage.getRecords()!=null&&productIPage.getRecords().size()>0){
            List<String> list=new ArrayList<>();

            for (int i=0;i<productIPage.getRecords().size();i++){

                List<String> values = productPropertyValueMapper.findByProperId(productIPage.getRecords().get(i).getProductPropertyId());
                System.out.println(values);
                productIPage.getRecords().get(i).setProductPropertyValueName(values);
            }

        }

        return productPropertyPage;
    }

    /**
     * 删除
     * @param ids
     */
    @Override
    public void deleteProductProperty(List<Integer> ids) {
        productPropertyMapper.deleteProductProperty(ids);
    }

}
